from eventServer import eventServer
from cCube import CCube
from vec2 import Vec2

class MovingPlatform(CCube):
    
    def __init__(self):
        CCube.__init__(self)
        
        eventServer.registerToFront("update", self.update)
        self.setVar("vel", Vec2(0, 0))
        self.setVar("range", 0)
        self.setVar("home", Vec2())
        self.setVar("base", "movingPlatform")
        
    def update(self, cmd, args):
        v = self.getVar("vel")
        p = self.getVar("pos")
        h = self.getVar("home")
        r = self.getVar("range")

        dt = args["dtime"]
        if(dt > 1 or dt < 0):
            dt = 0
    
        p.x += dt * v.x
        p.y += dt * v.y
        
        dV = h + (v * r)
        d = (dV.x - p.x)**2 + (dV.y - p.y)**2
        if(d < 0.5):
            v.x = v.x * -1
            v.y = v.y * -1